<?php

class m_bagian extends CI_Model {

    function __construct() {
        // Call the Model constructor
        parent::__construct();
    }

    function get_count_all_bagian() {
        $sql = "SELECT COUNT(bagian_cd)'total' FROM bagian";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    }

     function get_count_all_bagian_search($params) {
        $sql = "SELECT COUNT(bagian_cd)'total' FROM bagian
                WHERE bagian_name LIKE ? AND bagian_penanggungjawab LIKE ? ";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    }
    //cek data
    function get_count_all_bagian_by_kode($params) {
        $sql = "SELECT COUNT(*) 'total' FROM bagian WHERE bagian_cd = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    }

    //cek data
    function get_count_all_bagian_by_kode_nama($params) {
        $sql = "SELECT COUNT(*) 'total' FROM bagian WHERE bagian_cd = ? AND bagian_name = ? AND mdb =?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    }

    // get user profil
    function get_all_bagian_pagination($params) {
        $sql = "SELECT * FROM bagian
                ORDER BY bagian_cd
                LIMIT ?, ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

     function get_all_bagian_pagination_search($params) {
        $sql = "SELECT * FROM bagian
                WHERE bagian_name LIKE ? 
                AND bagian_penanggungjawab LIKE ?
                ORDER BY bagian_cd
                LIMIT ?, ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }
    function get_all_bagian() {
        $sql = "SELECT * FROM bagian
                ORDER BY bagian_name";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    function get_bagian_by_id($params) {
        $sql = "SELECT * FROM bagian WHERE bagian_cd = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    // insert
    function insert($user_id) {
        // update by this date
        $sql = "INSERT INTO bagian VALUES (?, ?, ?, ?, NOW())";
        return $this->db->query($sql, $user_id);
    }

    // edit
    function edit($user_id) {
        // update by this date
        $sql = "UPDATE bagian SET bagian_name = ?, bagian_penanggungjawab = ?, mdb = ? WHERE bagian_cd = ?";
        return $this->db->query($sql, $user_id);
    }

    // delete
    function delete($user_id) {
        // update by this date
        $sql = "DELETE FROM bagian WHERE bagian_cd = ?";
        return $this->db->query($sql, $user_id);
    }

}
